<?php
class WXBiz {
	private $appid = 'wx7a1bcd95ff9c42ba';  //正式会员
	//private $appid = 'wx569eaa5d88a0ccbb';

	private $appSecret = '1e55630a71661257044578fd994f45d1';//正式会员
	//private $appSecret = '4fd2a3a21542d888b2d7f30844d4cc31';

	public function getSession($code) {

		$apiUrl = sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", $this->appid, $this->appSecret, $code);

		//print_r($apiUrl);

		$apiData = json_decode($this->curlHttp($apiUrl, true), true);

		//print_r($apiData);

		if (isset($apiData['session_key'])) {

			return $apiData['session_key'];

		} else {

			return false;
		}

	}

	public function getOpenId($code) {

		$apiUrl = sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", $this->appid, $this->appSecret, $code);
		//print_r($apiUrl);

		$apiData = json_decode($this->curlHttp($apiUrl, true), true);

		//print_r($apiData);

		if (isset($apiData['openid'])) {

			return $apiData['openid'];

		} else {

			return false;
		}

	}
	
	public function accessToken() {
	    $apiUrl = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", $this->appid, $this->appSecret);
	
	    //print_r($apiUrl);
	
	    $apiData = json_decode($this->curlHttp($apiUrl, true), true);
	    \think\Log::record(json_encode ( $apiData ));
	    //print_r($apiData);
	
	    if (isset($apiData['access_token'])) {
	
	        return $apiData['access_token'];
	
	    } else {
	
	        return false;
	    }
	
	}
	
	
	
	public function sendNotice($accessToken,$data) {
	    $apiUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$accessToken";
	
	    //print_r($apiUrl);
	
	    $apiData = json_decode($this->curlHttp($apiUrl, true, true,$data), true);
	    \think\Log::record(json_encode ( $apiData ).'这是accessToken');
	    //print_r($apiData);
	
	    return $apiData;
	
	}
//生成二维码
	public function generateXCXQRWithImageId($imageId) {

		try {

			$url = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", $this->appid, $this->appSecret);

			$access_tokenArr = $this->getJsonArray($url, 'GET', '', '', '');

			if (isset($access_tokenArr->{'errcode'})) {

				return false;

			} else {

				$accessToken = $access_tokenArr->{'access_token'};

				$url = sprintf('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s', $accessToken);

				//$url = sprintf('https://api.weixin.qq.com/wxa/getwxacode?access_token=%s', $accessToken);

				$data = array('scene' => $imageId); //, 'page' => '/pages/gate/gate');
				//$data = array('path' => '/pages/gate/gate');

				$jsonData = urldecode(json_encode($data));

				return $this->getJsonArray($url, 'POST', $jsonData, 'yes', $imageId);

			}

		} catch (Exception $e) {

			return false;

		}
	}

	private function getJsonArray($url, $method, $jsonData, $qr, $imageId, $second = 30) {

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_TIMEOUT, $second);

		curl_setopt($ch, CURLOPT_URL, $url);

		curl_setopt($ch, CURLOPT_POST, 1);

		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

		$result = curl_exec($ch);

		//print_r($result);

		if ($result) {

			curl_close($ch);

			if ($qr == 'yes') {

				$path = 'C:/ST/develop.stholding.cn/memberphotos/linkface/qrcode/' . $imageId . '.jpg';

				$resutlUrl = 'https://develop.stholding.cn/memberphotos/linkface/qrcode/' . $imageId . '.jpg';

				if (file_put_contents($path, $result)) {

					return $resutlUrl;

				} else {

					return false;

				}

			} else {

				return json_decode($result);
			}

		} else {

			$error = curl_errno($ch);

			//echo "CURL错误码:$error"."<br>";

			//echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";

			curl_close($ch);

			return false;
		}
	}

	private function curlHttp($url, $https = false, $post = false, $post_data = array()) {
		$ch = curl_init(); //初始化一个curl
		curl_setopt($ch, CURLOPT_URL, $url); //设置接口地址  如：http://wwww.xxxx.co/api.php
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //是否把CRUL获取的内容赋值到变量
		curl_setopt($ch, CURLOPT_HEADER, 0); //是否需要响应头
		/*是否post提交数据*/
		if ($post) {
			curl_setopt($ch, CURLOPT_POST, 1);
			if (!empty($post_data)) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
			}
		}
		/*是否需要安全证书*/
		if ($https) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		}
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}

}